// 工人服务
const { v4 } = require('uuid');
const DbService = require('./db2');
const BaseService = require('./base');
const { TABLE_NAME } = require('../models');

class SendService extends BaseService {
  constructor() {
    super();
    this.dbService = new DbService(TABLE_NAME.send);
  }

  async create(data) {
    const record = {
      ...data,
      id: v4(), // 生成唯一id
      createdOn: new Date(),
    };

    await this.dbService.insert(record);
    return record;
  }
  async batchCreate(sendInfo) {
    const { workers, tenantId, startTime, endTime } = sendInfo;
    await Promise.all(
      workers.map(item => {
        const id = v4();
        return this.dbService.insert({
          id,
          workerId: item.id,
          tenantId,
          startTime: new Date(startTime),
          endTime: new Date(endTime),
          status: 'SEND',
          createdOn: new Date(),
        });
      }),
    );
    return true;
  }

  async batchUpdate(list) {
    try {
      if (this.dbService) {
        await Promise.all(
          list.map(item => this.dbService.update(item.Id || item.id, { status: 'WAIT_SEND' })),
        );
        return true;
      }
      return true;
    } catch (error) {
      console.log('error:', list, error);
      return false;
    }
  }
  // 收回
  async batchBack(list) {
    try {
      if (this.dbService) {
        if (this.dbService) {
          await Promise.all(

            list.map(item => this.dbService.update(item.Id || item.id, { status: 'BACK' })),
          );
          return true;
        }
        return true;
      }
      return true;
    } catch (error) {
      console.log('error:', list, error);
      return false;
    }
  }

  async list() {
    const workerData = await this.dbService.selectAll();
    return workerData;
  }
}

module.exports = SendService;
